﻿package ua.mihaylenko.cardsplugin.components.thumbs{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.display.SimpleButton;
	import flash.display.Stage;
	
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import flash.net.URLRequest;
	
	import flash.geom.Rectangle;
		
	import flash.text.TextField;
	
	import fl.transitions.Tween;
	import fl.transitions.easing.Back;
	import fl.transitions.easing.Bounce;	
	import ua.mihaylenko.cardsplugin.data.DataThumb;
	
	public class ThumbItem extends Sprite{
		public static var _iThumbSettings:Object;
		public var _sdtTitle:TextField;
		public var _btnHander:SimpleButton;
		public var _smcPreload:MovieClip;
		public var _selectIcon:MovieClip;
		private var _bSelect:Boolean;
		private var twInOut:Tween;
		private var _cDataThumb:DataThumb;
		private var _oItemEvent:Object;
		private var _border:Sprite;
		private var _index:uint;
		public function ThumbItem(cData:DataThumb, index:uint)
		{
			//make the selection icon invisible
			_selectIcon.visible=false;
			
			_cDataThumb = cData;
			_index = index;
			_oItemEvent = new Object();
			_sdtTitle.mouseEnabled = false;
			if (cData.title)
			{
				_sdtTitle.text = cData.title;
			}
			else
			{
				_sdtTitle.text = "";
			}
			if (!_iThumbSettings)
			{
				_iThumbSettings = new Object();
				_iThumbSettings.x = 2.0;
				_iThumbSettings.y = 2.0;
				_iThumbSettings.width = 77.3;
				_iThumbSettings.height = 52.0;
			}
			var cLoader:Loader = new Loader();
			addListeners(cLoader.contentLoaderInfo);
			try{
				cLoader.load(new URLRequest(cData.thumb));
			}catch(error:Error){
				removeListeners(cLoader.contentLoaderInfo);
			}
			_smcPreload.gotoAndPlay(2);
		}
		public function set select(bSelect:Boolean)
		{
			_bSelect = bSelect;
			if (!_border) return;
			_border.graphics.clear();
			if (_bSelect)
			{
				_border.graphics.lineStyle(3, 0xFF0000);
				_selectIcon.visible=true;
				swapChildren(_selectIcon,getChildAt(numChildren-1));
			}
			else
			{
				_border.graphics.lineStyle(1, 0xFFFFFFF);
				_selectIcon.visible=false;
			}
			_border.graphics.moveTo(0, 0);
			_border.graphics.lineTo(_iThumbSettings.width + _iThumbSettings.x, 0);
			_border.graphics.lineTo(_iThumbSettings.width + _iThumbSettings.x, _iThumbSettings.height + _iThumbSettings.y);
			_border.graphics.lineTo(0, _iThumbSettings.height + _iThumbSettings.y);
			_border.graphics.lineTo(0, 0);			
		}
		public function get select():Boolean
		{
			return _bSelect;
		}
		private function addListeners(dispatcher:EventDispatcher)
		{
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		private function removeListeners(dispatcher:EventDispatcher)
		{
            dispatcher.removeEventListener(Event.INIT, initHandler);
            dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		public function get data():DataThumb
		{
			return _cDataThumb;
		}
		private function initHandler(oEvent:Event)
		{
			var _bmPreview:Bitmap = (oEvent.target.content as Bitmap);	
			var nPercent:Number = _bmPreview.width/_bmPreview.height;
			var nScaleW:Number = _iThumbSettings.width/_bmPreview.width;
			var nScaleH:Number = _iThumbSettings.height/_bmPreview.height;
			if (nScaleW < nScaleH)
			{
				_bmPreview.width *= nScaleW;
				_bmPreview.height = _bmPreview.width / nPercent;
			}
			else
			{
				_bmPreview.height *= nScaleH;
				_bmPreview.width = _bmPreview.height * nPercent;
			}
			var nFreeWidth:Number = -(_bmPreview.width - _iThumbSettings.width)/2;
			var nFreeHeight:Number = -(_bmPreview.height - _iThumbSettings.height)/2;
			if(nFreeWidth>0)nFreeWidth = nFreeWidth+1; else nFreeWidth = 1;
			if(nFreeHeight>0)nFreeHeight = nFreeHeight+1;else nFreeHeight = 1;
			_bmPreview.x = nFreeWidth;
			_bmPreview.y = nFreeHeight;				
//			var bmThumb:Bitmap = new Bitmap(bmdThumb, "auto", true);
//			bmThumb.x = _iThumbSettings.x
//			bmThumb.y = _iThumbSettings.y;
			this.addChild(_bmPreview);
			_btnHander.addEventListener(MouseEvent.MOUSE_OVER, onMouseHandler);
			_btnHander.addEventListener(MouseEvent.MOUSE_OUT, onMouseHandler);
			_btnHander.addEventListener(MouseEvent.CLICK, onMouseHandler);
			_smcPreload.stop();
			this.removeChild(_smcPreload);
			//
			_border = new Sprite();
			_border.graphics.lineStyle(1, 0xFFFFFFF);
			_border.graphics.moveTo(0, 0);
			_border.graphics.lineTo(_iThumbSettings.width + _iThumbSettings.x, 0);
			_border.graphics.lineTo(_iThumbSettings.width + _iThumbSettings.x, _iThumbSettings.height + _iThumbSettings.y);
			_border.graphics.lineTo(0, _iThumbSettings.height + _iThumbSettings.y);
			_border.graphics.lineTo(0, 0);
			this.addChild(_border);
			this.select = _bSelect;
			//trace(_cDataThumb.selected);
		}
		private function progressHandler(oEvent:ProgressEvent)
		{
			
		}
		public function close()
		{
			if (!select && this.y < 0 )
			{
				if (twInOut)
				{
					twInOut.stop();
				}
				twInOut = new Tween(this, "y",Bounce.easeInOut, this.y, 0, 5);
			}
		}
		private function ioErrorHandler(oEvent:IOErrorEvent){
			removeListeners((oEvent.target as Loader).contentLoaderInfo);
			(oEvent.target as Loader).unload();
		}
		public function AddListener(sEvent:String, oEvent:Object)
		{
			_oItemEvent[sEvent] = oEvent;
		}
		public function get id():String
		{
			return data.id;
		}
		public function get description():String
		{
			return data.description;
		}
		public function get index():uint
		{
			return _index;
		}
		private function onMouseHandler(oEvent:MouseEvent)
		{
			if(_oItemEvent[oEvent.type]){
				_oItemEvent[oEvent.type]._function.apply(_oItemEvent[oEvent.type]._object, [this]);
			}
			switch(oEvent.type){
				case MouseEvent.MOUSE_OVER:
					// disable popup the thumbs
					/*
					if(!select && this.y > -(this.height/3) ){
						if(twInOut){
							twInOut.stop();
						}
						twInOut = new Tween(this, "y",Bounce.easeInOut, this.y, -(this.height/5), 5);
					}
					*/
					break;
			}
		}
	}
}